package com.mfangsoft.zhuangjialong.app.appNewActivity.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mfangsoft.zhuangjialong.app.appNewActivity.model.CustomerSignUpInfo;
import com.mfangsoft.zhuangjialong.app.weixin.mapper.WeiXinMapper;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mfangsoft.zhuangjialong.app.appNewActivity.mapper.AppNewActivityMapper;
import com.mfangsoft.zhuangjialong.app.appNewActivity.service.IAppNewActivityService;
import com.mfangsoft.zhuangjialong.common.model.Page;

@Service
public class AppNewActivityServiceImpl implements IAppNewActivityService {

    @Autowired
    AppNewActivityMapper appNewActivityMapper;

    @Autowired
    WeiXinMapper weiXinMapper;

    @Override
    public Map<String, Object> selectNearestFiliale(Map<String, Object> map) {
        Map<String, Object> filiale = appNewActivityMapper.selectNearestFiliale(map);
//		if(filiale!=null){
//			if(filiale.get("sys_user_id")!=null){
//				Map<String, Object> filialeById = appNewActivityMapper.selectFilialeById(Integer.valueOf(filiale.get("sys_user_id").toString()));
//				return filialeById;
//			}
//		}
        return filiale;
    }

    @Override
    public Map<String, Object> selectFilialeActivity(Integer filialeId, Integer customer_id) {
        List<Map<String, Object>> filialeActivity = appNewActivityMapper.selectFilialeActivity(filialeId);
        if (filialeActivity != null && filialeActivity.size() > 0) {
            Map<String, Object> map = filialeActivity.get(0);
            List<String> applyImage = appNewActivityMapper.selectActivityApplyImage(Integer.valueOf(map.get("activity_id").toString()));
            map.put("applyImage", applyImage);
            List<Map<String, Object>> previousActivity = appNewActivityMapper.selectPreviousActivity(Integer.valueOf(map.get("user_id").toString()));
            map.put("previousActivity", previousActivity);
            Map<String, Object> m = new HashMap<>();
            m.put("customer_id", customer_id);
            m.put("activity_id", map.get("activity_id"));
            Integer isApply = appNewActivityMapper.selectIsApply(m);
            if (isApply > 0) {
                map.put("isApply", 1);
            } else {
                map.put("isApply", 2);
            }
            appNewActivityMapper.updateActivityLook(Integer.valueOf(map.get("activity_id").toString()));
            return map;
        }
        return null;
    }

    @Override
    public Map<String, Object> selectCustomerCodeActivity(Integer customer_id) {
        List<Map<String, Object>> applyActivity = appNewActivityMapper.selectCustomerApplyActivity(customer_id);
        if (applyActivity != null && applyActivity.size() > 0) {
            Map<String, Object> map = applyActivity.get(0);
            map.put("isApply", 1);
            List<String> applyImage = appNewActivityMapper.selectActivityApplyImage(Integer.valueOf(map.get("activity_id").toString()));
            map.put("applyImage", applyImage);
            List<Map<String, Object>> previousActivity = appNewActivityMapper.selectPreviousActivity(Integer.valueOf(map.get("user_id").toString()));
            map.put("previousActivity", previousActivity);
            return map;
        } else {
            Integer relation = appNewActivityMapper.selectCustomerRelation(customer_id);
            if (relation > 0) {
                List<Map<String, Object>> customerCodeActivity = appNewActivityMapper.selectCustomerCodeActivity(customer_id);
                if (customerCodeActivity != null && customerCodeActivity.size() > 0) {
                    Map<String, Object> map = customerCodeActivity.get(0);
                    List<String> applyImage = appNewActivityMapper.selectActivityApplyImage(Integer.valueOf(map.get("activity_id").toString()));
                    map.put("applyImage", applyImage);
                    List<Map<String, Object>> previousActivity = appNewActivityMapper.selectPreviousActivity(Integer.valueOf(map.get("user_id").toString()));
                    map.put("previousActivity", previousActivity);
                    Map<String, Object> m = new HashMap<>();
                    m.put("customer_id", customer_id);
                    m.put("activity_id", map.get("activity_id"));
                    Integer isApply = appNewActivityMapper.selectIsApply(m);
                    if (isApply > 0) {
                        map.put("isApply", 1);
                    } else {
                        map.put("isApply", 2);
                    }
                    appNewActivityMapper.updateActivityLook(Integer.valueOf(map.get("activity_id").toString()));
                    return map;
                }
            } else {
                String regionCode = appNewActivityMapper.selectCustomerRegionCode(customer_id);
                if (regionCode != null && !"".equals(regionCode)) {
                    String[] split = regionCode.split(",");
                    List<Integer> code = appNewActivityMapper.selectShopByCode(split[1]);
                    if (code != null && code.size() > 0) {
                        Integer node2 = appNewActivityMapper.selectNode2ByNode1(code.get(0));
                        return selectFilialeActivity(node2, customer_id);
                    }
                }
            }
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> selectActivityShop(Integer activity_id) {
        List<Map<String, Object>> activityShop = appNewActivityMapper.selectActivityShop(activity_id);
        return activityShop;
    }

    @Override
    public Map<String, Object> selectPreviousActivityDrtail(Integer activity_id) {
        Map<String, Object> activityDrtail = appNewActivityMapper.selectPreviousActivityDrtail(activity_id);
        List<String> applyImage = appNewActivityMapper.selectActivityApplyImage(activity_id);
        activityDrtail.put("applyImage", applyImage);
        return activityDrtail;
    }

    @Override
    public Page<Map<String, Object>> selectActivityCommentPage(Page<Map<String, Object>> page) {
        List<Map<String, Object>> commentPage = appNewActivityMapper.selectActivityCommentPage(page);
        page.setData(commentPage);
        return page;
    }

    @Override
    public Page<Map<String, Object>> selectCustomerActivityCommentPage(Page<Map<String, Object>> page) {
        List<Map<String, Object>> commentPage = appNewActivityMapper.selectCustomerActivityCommentPage(page);
        page.setData(commentPage);
        return page;
    }

    @Override
    public Map<String, Object> selectFilialeByCode(Map<String, Object> map) {
        List<Map<String, Object>> filialeByCode = appNewActivityMapper.selectFilialeByCode(map);
        if (filialeByCode != null && filialeByCode.size() > 0) {
            Map<String, Object> map2 = filialeByCode.get(0);
            return map2;
        }
        return null;
    }

    @Override
    public Map<String, Object> selectFilialeActivityByCode(Map<String, Object> map) {
        List<Map<String, Object>> filialeActivity = appNewActivityMapper.selectFilialeActivityByCode(map.get("filialeId") == null || "".equals(map.get("filialeId")) ? 0 : Integer.valueOf(map.get("filialeId").toString()));
        if (filialeActivity != null && filialeActivity.size() > 0) {
            Map<String, Object> map3 = filialeActivity.get(0);
            return map3;
        } else {
            return null;
        }
    }

    @Override
    public ResponseMessage<String> addCustomerByActivitySignUp(CustomerSignUpInfo customerSignUpInfo) {
        ResponseMessage<String> message = new ResponseMessage<>();
        if (customerSignUpInfo.getName() == null || "".equals(customerSignUpInfo.getName())) {
            message.setCode("2");
            message.setMessage("姓名不能为空！");
            return message;
        }
        if (customerSignUpInfo.getPhone() == null || "".equals(customerSignUpInfo.getPhone())) {
            message.setCode("3");
            message.setMessage("手机号不能为空！");
            return message;
        }
        if (customerSignUpInfo.getRegionCode() == null || "".equals(customerSignUpInfo.getRegionCode())) {
            message.setCode("4");
            message.setMessage("省市区不能为空！");
            return message;
        }
        Integer num = weiXinMapper.selectCustomerNumByPhone(customerSignUpInfo.getPhone());
        if (num > 0) {
            message.setCode("5");
            message.setMessage("该手机号已存在！");
            return message;
        }
        String[] regionCode = customerSignUpInfo.getRegionCode().split(",");
        if (regionCode.length <2) {
            message.setCode("6");
            message.setMessage("省市区不合法！");
            return message;
        }
        Map<String, Object> customer = new HashMap<>();
        customer.put("phone", customerSignUpInfo.getPhone());
        customer.put("name", customerSignUpInfo.getName());
        customer.put("source", 99);
        customer.put("province", regionCode[0]);
        customer.put("city", regionCode[1]);
        customer.put("property", 1);
        customer.put("remark", customerSignUpInfo.getRemark());
        weiXinMapper.addCustomer(customer);
        List<String> filialeId = weiXinMapper.selectFilialeIdByAddress(regionCode[1]);
        if (filialeId != null && filialeId.size() > 0) {
            Map<String, Object> customerSeller = new HashMap<>();
            customerSeller.put("customerId", customer.get("user_id"));
            customerSeller.put("filiale", filialeId.get(0));
            weiXinMapper.addCustomerSeller(customerSeller);
        } else {
            List<Integer> code = appNewActivityMapper.selectShopByCode(regionCode[1]);
            if (code != null && code.size() > 0) {
                Integer node2 = appNewActivityMapper.selectNode2ByNode1(code.get(0));
                Map<String, Object> customerSeller = new HashMap<>();
                customerSeller.put("customerId", customer.get("user_id"));
                customerSeller.put("filiale", node2);
                weiXinMapper.addCustomerSeller(customerSeller);
            }
        }
        message.setCode(SysConstant.SUCCESS_CODE);
        message.setMessage(SysConstant.SUCCESS_MSG);
        return message;
    }
}
